{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Last Visited Integers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: lastVisitedIntegers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #上一个遍历的整数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的字符串数组&nbsp;<code>words</code>&nbsp;，其中&nbsp;<code>words[i]</code>&nbsp;要么是一个字符串形式的正整数，要么是字符串&nbsp;<code>\"prev\"</code>&nbsp;。</p>\n",
    "\n",
    "<p>我们从数组的开头开始遍历，对于 <code>words</code>&nbsp;中的每个&nbsp;<code>\"prev\"</code>&nbsp;字符串，找到 <code>words</code>&nbsp;中的 <strong>上一个遍历的整数</strong>&nbsp;，定义如下：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>k</code>&nbsp;表示到当前位置为止的连续&nbsp;<code>\"prev\"</code>&nbsp;字符串数目（包含当前字符串），令下标从&nbsp;<strong>0</strong>&nbsp;开始的&nbsp;<strong>整数</strong> 数组&nbsp;<code>nums</code>&nbsp;表示目前为止遍历过的所有整数，同时用&nbsp;<code>nums_reverse</code>&nbsp;表示&nbsp;<code>nums</code>&nbsp;反转得到的数组，那么当前 <code>\"prev\"</code>&nbsp;对应的 <strong>上一个遍历的整数</strong>&nbsp;是&nbsp;<code>nums_reverse</code>&nbsp;数组中下标为 <code>(k - 1)</code>&nbsp;的整数。</li>\n",
    "\t<li>如果&nbsp;<code>k</code>&nbsp;比目前为止遍历过的整数数目 <strong>更多</strong>&nbsp;，那么上一个遍历的整数为&nbsp;<code>-1</code>&nbsp;。</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你返回一个整数数组，包含所有上一个遍历的整数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong class=\"example\">示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b><code>words</code> = [\"1\",\"2\",\"prev\",\"prev\",\"prev\"]\n",
    "<b>输出：</b>[2,1,-1]\n",
    "<b>解释：</b>\n",
    "对于下标为 2 处的 \"prev\" ，上一个遍历的整数是 2 ，因为连续 \"prev\" 数目为 1 ，同时在数组 reverse_nums 中，第一个元素是 2 。\n",
    "对于下标为 3 处的 \"prev\" ，上一个遍历的整数是 1 ，因为连续 \"prev\" 数目为 2 ，同时在数组 reverse_nums 中，第二个元素是 1 。\n",
    "对于下标为 4 处的 \"prev\" ，上一个遍历的整数是 -1 ，因为连续 \"prev\" 数目为 3 ，但总共只遍历过 2 个整数。\n",
    "</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b><code>words</code> = [\"1\",\"prev\",\"2\",\"prev\",\"prev\"]\n",
    "<b>输出：</b>[1,2,1]\n",
    "<strong>解释：</strong>\n",
    "对于下标为 1 处的 \"prev\" ，上一个遍历的整数是 1 。\n",
    "对于下标为 3 处的 \"prev\" ，上一个遍历的整数是 2 。\n",
    "对于下标为 4 处的 \"prev\" ，上一个遍历的整数是 1 ，因为连续 \"prev\"<strong>&nbsp;</strong>数目为 2 ，同时在数组 reverse_nums 中，第二个元素是 1 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= words.length &lt;= 100</code></li>\n",
    "\t<li><code>words[i] == \"prev\"</code>&nbsp;或&nbsp;<code>1 &lt;= int(words[i]) &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [last-visited-integers](https://leetcode.cn/problems/last-visited-integers/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [last-visited-integers](https://leetcode.cn/problems/last-visited-integers/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"1\",\"2\",\"prev\",\"prev\",\"prev\"]', '[\"1\",\"prev\",\"2\",\"prev\",\"prev\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        n = len(words)                                                      \n",
    "        res = []                                                            \n",
    "        arr = []                                                            \n",
    "        flag = 0                                                            \n",
    "        for i, x in enumerate(words):                                       \n",
    "            if x != 'prev':                                                 \n",
    "                flag = 0                                                    \n",
    "                arr.append(int(x))                                          \n",
    "            else:                                                           \n",
    "                flag += 1                                                   \n",
    "                res.append(arr[-flag] if flag <= len(arr) else -1)          \n",
    "        return res                                                          \n",
    "                                                                                                           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        nums = []\n",
    "        for word in words:\n",
    "            if \"prev\" == word:\n",
    "                cnt +=1\n",
    "                if cnt > len(nums):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(nums[len(nums)-cnt])\n",
    "            else:\n",
    "                cnt = 0\n",
    "                nums.append(int(word))\n",
    "        return ans\n",
    "\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        # 1 统计连续prev的数量\n",
    "        # 2 往前找整数\n",
    "        ans = []\n",
    "        for i, w in enumerate(words):\n",
    "            if w == \"prev\":\n",
    "                # 计算连续prev的数量\n",
    "                j = i - 1\n",
    "                while j >= 0 and words[j] == \"prev\":\n",
    "                    j -= 1\n",
    "                cnt = i - j\n",
    "                temp = [t for t in words[0:i] if t != \"prev\"][::-1]\n",
    "                ans.append(int(temp[cnt - 1]) if cnt <= len(temp) else -1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "\n",
    "        c = 0\n",
    "        p = []\n",
    "        n = 0\n",
    "        ans = []\n",
    "        for w in words:\n",
    "            if w == 'prev':\n",
    "                c += 1\n",
    "                if c > n: ans.append(-1)\n",
    "                else: ans.append(p[n-c])\n",
    "            else:\n",
    "                c = 0\n",
    "                p.append(int(w))\n",
    "                n += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums_reverse_list = list()\n",
    "        answer_list = list()\n",
    "        prev_count = 0\n",
    "        for word in words:\n",
    "            if word[0] == 'p':\n",
    "                prev_count += 1\n",
    "                if prev_count > len(nums_reverse_list):\n",
    "                    answer_list.append(-1)\n",
    "                else:\n",
    "                    answer_list.append(nums_reverse_list[-prev_count])\n",
    "            else:\n",
    "                nums_reverse_list.append(int(word))\n",
    "                prev_count = 0\n",
    "        return answer_list\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums = []\n",
    "        k = 0\n",
    "        ans = []\n",
    "        for w in words:\n",
    "            if w[0] != 'p':\n",
    "                nums.append(int(w))\n",
    "                k = 0\n",
    "            else:\n",
    "                k += 1\n",
    "                ans.append(nums[-k] if k <= len(nums) else -1)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        k = 0\n",
    "        res = []\n",
    "        nums = []\n",
    "        for word in words:\n",
    "            if word == \"prev\":\n",
    "                k += 1\n",
    "                if k > len(nums):\n",
    "                    res.append(-1)\n",
    "                else:\n",
    "                    res.append(nums[-k])\n",
    "                # k += 1\n",
    "            else:\n",
    "                nums.append(int(word))\n",
    "                k = 0\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        theList = []\n",
    "        record = []\n",
    "        index = 0\n",
    "        for word in words:\n",
    "            if word != \"prev\":\n",
    "                theList.append(int(word))\n",
    "                index = 0\n",
    "            else:\n",
    "                curIndex = len(theList) - 1 - index\n",
    "                if curIndex < 0:\n",
    "                    record.append(-1)\n",
    "                else:\n",
    "                    record.append(theList[curIndex])\n",
    "                index += 1\n",
    "        return record    \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        res=list()\n",
    "        nums=[-1]\n",
    "        cur=0\n",
    "        flag=False\n",
    "        for w in words:\n",
    "            if w==\"prev\":\n",
    "                if not flag:\n",
    "                    res.append(nums[cur])\n",
    "                else:\n",
    "                    cur=max(0,cur-1)\n",
    "                    res.append(nums[cur])\n",
    "                flag=True\n",
    "                #res.append(nums[cur])\n",
    "                \n",
    "                #flag=True\n",
    "                \n",
    "                #cur=max(0,cur-1)\n",
    "            else:\n",
    "                flag=False\n",
    "                nums.append(int(w))\n",
    "                cur=len(nums)-1\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        cnt=[]\n",
    "        prev=0\n",
    "        ans=[]\n",
    "        for i in words:\n",
    "            if i==\"prev\":\n",
    "                prev+=1\n",
    "                if prev>len(cnt):\n",
    "                    ans+=[-1]\n",
    "                else:\n",
    "                    ans+=[int(cnt[-prev])]\n",
    "            else:\n",
    "                prev=0\n",
    "                cnt+=[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        \"\"\"模拟\"\"\"\n",
    "        prevCnt=0  # 记录prev个数\n",
    "        nums=[]  # 记录遍历过的数字\n",
    "        n=0  # nums的长度\n",
    "        ans=[]  # 答案\n",
    "        for x in words:\n",
    "            if x == 'prev':\n",
    "                prevCnt += 1\n",
    "                k = prevCnt-1\n",
    "                if k<n:\n",
    "                    ans.append(nums[n-k-1])\n",
    "                else:\n",
    "                    ans.append(-1)\n",
    "            else:\n",
    "                nums.append(int(x))\n",
    "                n+=1\n",
    "                prevCnt=0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        A = []\n",
    "        ret = []\n",
    "        idx = -1\n",
    "        for w in words:\n",
    "            if w != \"prev\":\n",
    "                A.append(int(w))\n",
    "                idx = len(A) - 1\n",
    "            else:\n",
    "                ret.append(A[idx] if idx >= 0 else -1)\n",
    "                idx -= 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        stack = []\n",
    "        ret = []\n",
    "        prev = 1\n",
    "        for i in words:\n",
    "            if i != \"prev\":\n",
    "                stack.append(int(i))\n",
    "                prev = 1\n",
    "            else:\n",
    "                if prev > len(stack):\n",
    "                    ret.append(-1)\n",
    "                else:\n",
    "                    ret.append(stack[-prev])\n",
    "                prev += 1\n",
    "            #print(ret)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        stk = []\n",
    "        prv = []\n",
    "        ans = []\n",
    "        last_word = \"\"\n",
    "        for w in words:\n",
    "            if w == \"prev\":\n",
    "                depth = prv[-1]+1 if w == last_word else 1\n",
    "                prv.append(depth)\n",
    "                ans.append(stk[len(stk)-prv[-1]] if prv[-1] <= len(stk) else -1)   \n",
    "            else:\n",
    "                stk.append(int(w))\n",
    "            last_word = w\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        res=[]\n",
    "        ans=[]\n",
    "        k=0\n",
    "        for w in words:\n",
    "            if w!='prev':\n",
    "                res.append(int(w))\n",
    "                k=0\n",
    "            else:\n",
    "                k+=1\n",
    "                ans.append(-1 if k>len(res) else res[-k])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        l=[]\n",
    "        res=[]\n",
    "        count=0\n",
    "        for s in words:\n",
    "            if s==\"prev\":\n",
    "                count+=1\n",
    "                if count>len(l):\n",
    "                    res.append(-1)\n",
    "                else:\n",
    "                    res.append(l[-count])\n",
    "            else:\n",
    "                count=0\n",
    "                l.append(int(s))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        num_list = []\n",
    "        ret_list = []\n",
    "        c = 0\n",
    "        for word in words:\n",
    "            if word != 'prev':\n",
    "                num_list.append(int(word))\n",
    "                c = 0\n",
    "            else:\n",
    "                c += 1\n",
    "                if c > len(num_list):\n",
    "                    ret_list.append(-1)\n",
    "                else:\n",
    "                    ret_list.append(num_list[-c])\n",
    "        return ret_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        k = 0\n",
    "        stk = []\n",
    "        ans = []\n",
    "        for w in words:\n",
    "            if w == 'prev':\n",
    "                k += 1\n",
    "                if k > len(stk):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(stk[-k])\n",
    "            else:\n",
    "                k = 0\n",
    "                stk.append(int(w))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        res = []\n",
    "        nums = []\n",
    "        k = 0\n",
    "        for word in words:\n",
    "            if word==\"prev\":\n",
    "                k += 1\n",
    "                nums.reverse()\n",
    "                if k-1<len(nums):\n",
    "                    res.append(nums[k-1])\n",
    "                else:\n",
    "                    res.append(-1)\n",
    "                nums.reverse()\n",
    "\n",
    "            else:\n",
    "                k=0\n",
    "                nums.append(int(word))\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        res = []\n",
    "        arr = []\n",
    "        t = 0\n",
    "        for word in words:\n",
    "            if word == 'prev':\n",
    "                t -= 1\n",
    "                if abs(t) <= len(arr):\n",
    "                    res.append(arr[t])\n",
    "                else:\n",
    "                    res.append(-1)\n",
    "            else:\n",
    "                t = 0\n",
    "                arr.append(int(word))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        st = []\n",
    "        cnt = 0\n",
    "        for i, x in enumerate(words):\n",
    "            if x == 'prev':\n",
    "                cnt += 1\n",
    "                if cnt > len(st):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(int(st[-cnt]))\n",
    "            else:\n",
    "                st.append(x)\n",
    "                cnt = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        nums = []\n",
    "        n = len(words)\n",
    "        k = 0\n",
    "        for i in range(n):\n",
    "            if words[i].isdigit():\n",
    "                nums.append(int(words[i]))\n",
    "                k = 0\n",
    "                continue\n",
    "            k += 1\n",
    "            ans.append(-1 if k > len(nums) else nums[len(nums)-k])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        a=[]\n",
    "        ans=[]\n",
    "        cnt=0\n",
    "        for w in words:\n",
    "            if w==\"prev\":\n",
    "                cnt+=1\n",
    "                if cnt>len(a):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(a[-cnt])\n",
    "            else:\n",
    "                cnt=0\n",
    "                a.append(int(w))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        reversenum = []\n",
    "        for idx, item in enumerate(words):\n",
    "            if item == 'prev':\n",
    "                prenum = 1\n",
    "                for j in range(idx-1, -1, -1):\n",
    "                    if words[j] != 'prev':\n",
    "                        break\n",
    "                    else:\n",
    "                        prenum += 1\n",
    "                if prenum > len(reversenum):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(reversenum[len(reversenum)-prenum])\n",
    "            else:\n",
    "                reversenum.append(int(item))\n",
    "        return ans\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        q =[]\n",
    "        cnt = 0\n",
    "        res = []\n",
    "        for x in words:\n",
    "            if x.isdigit():\n",
    "                q.append(int(x))\n",
    "                cnt = 0\n",
    "            else:\n",
    "                cnt -= 1\n",
    "                if not q or len(q) < -cnt:\n",
    "                    res.append(-1)\n",
    "                else:\n",
    "                    res.append(q[cnt])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        nums = []\n",
    "        k = 0\n",
    "        for s in words:\n",
    "            if s[0] != 'p':  # 不是 prev\n",
    "                nums.append(int(s))\n",
    "                k = 0\n",
    "            else:\n",
    "                k += 1\n",
    "                ans.append(-1 if k > len(nums) else nums[-k])  # 倒数第 k 个\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        n = len(words)\n",
    "        i = 0\n",
    "        j = 0\n",
    "\n",
    "        ret = []\n",
    "\n",
    "        while i < n and j < n:\n",
    "            if words[j] != 'prev':\n",
    "                j += 1\n",
    "                i = j\n",
    "            else:\n",
    "                i -= 1\n",
    "                while i > -1 and words[i] == 'prev':\n",
    "                    i -= 1\n",
    "\n",
    "                if i > -1:\n",
    "                    ret.append(int(words[i]))\n",
    "                else:\n",
    "                    ret.append(-1)\n",
    "\n",
    "                j += 1\n",
    "\n",
    "        return ret\n",
    "\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        l=[]\n",
    "        x=-1\n",
    "        ans=[]\n",
    "        for i in words:\n",
    "            try:\n",
    "                l.append(int(i))\n",
    "                x=-1\n",
    "            except:\n",
    "                if x+len(l)>=0:\n",
    "                    ans.append(l[x])\n",
    "                    x-=1\n",
    "                else:\n",
    "                    ans.append(-1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        st = []\n",
    "        res = []\n",
    "        i = 0\n",
    "        for w in words:\n",
    "            if w == \"prev\":\n",
    "                if len(st) > i:\n",
    "                    res.append(st[i])\n",
    "                    i += 1\n",
    "                else:\n",
    "                    res.append(-1)\n",
    "            else:\n",
    "                st.insert(0, int(w))\n",
    "                i = 0\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans=[]\n",
    "        li=[]\n",
    "        i=0\n",
    "        for s in words:\n",
    "            if s==\"prev\":\n",
    "                i+=1\n",
    "                if len(li)>=i:ans.append(int(li[len(li)-i]))\n",
    "                else:ans.append(-1)\n",
    "            else:\n",
    "                i=0\n",
    "                li.append(int(s))\n",
    "        return ans;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        nums = []\n",
    "        prev_cnt = 0\n",
    "        for i, w in enumerate(words):\n",
    "            if w == \"prev\":\n",
    "                if i > 0 and words[i-1] == 'prev':\n",
    "                    prev_cnt += 1\n",
    "                else:\n",
    "                    prev_cnt = 1\n",
    "                \n",
    "                if prev_cnt <= len(nums):\n",
    "                    ans.append(nums[-prev_cnt])\n",
    "                else:\n",
    "                    ans.append(-1)\n",
    "            else:\n",
    "                nums.append(int(w))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        res = []\n",
    "        nums = []\n",
    "        k = 0\n",
    "\n",
    "        for word in words:\n",
    "            if word == \"prev\":\n",
    "                k += 1\n",
    "                res.append(-1 if k > len(nums) else nums[-k])\n",
    "            else:\n",
    "                k = 0\n",
    "                nums.append(int(word))\n",
    "\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums_reverse_list = list()\n",
    "        answer_list = list()\n",
    "        prev_count = 0\n",
    "        for word in words:\n",
    "            if word[0] == 'p':\n",
    "                prev_count += 1\n",
    "                if prev_count > len(nums_reverse_list):\n",
    "                    answer_list.append(-1)\n",
    "                else:\n",
    "                    answer_list.append(nums_reverse_list[-prev_count])\n",
    "            else:\n",
    "                nums_reverse_list.append(int(word))\n",
    "                prev_count = 0\n",
    "        return answer_list\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans=[]\n",
    "        k=0\n",
    "        nums=deque()\n",
    "        for word in words:\n",
    "            if word==\"prev\":\n",
    "                k+=1\n",
    "                if k>len(nums):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(int(nums[k-1]))\n",
    "                \n",
    "            else:\n",
    "                nums.appendleft(word)\n",
    "                k=0\n",
    "\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        s = []\n",
    "        needle = -1\n",
    "        for i in words:\n",
    "            if i == 'prev':\n",
    "                if abs(needle) <= len(s):\n",
    "                    ans.append(s[needle])\n",
    "                    needle -= 1\n",
    "                else:\n",
    "                    ans.append(-1)\n",
    "            else:\n",
    "                s.append(int(i))\n",
    "                needle = -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        st = []\n",
    "        ans = []\n",
    "        k = 0\n",
    "        for w in words:\n",
    "            if w == \"prev\":\n",
    "                k += 1\n",
    "                if k <= len(st):\n",
    "                    ans.append(st[-k])\n",
    "                else:\n",
    "                    ans.append(-1)\n",
    "            else:\n",
    "                k = 0\n",
    "                st.append(int(w))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        prevCnt=0\n",
    "        nums=[]\n",
    "        ans=[]\n",
    "        for x in words:\n",
    "            if x == 'prev':\n",
    "                prevCnt +=1\n",
    "                if prevCnt-1<len(nums):\n",
    "                    ans.append(nums[::-1][prevCnt-1])\n",
    "                else:\n",
    "                    ans.append(-1)\n",
    "            else:\n",
    "                nums.append(int(x))\n",
    "                prevCnt=0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        stack = []\n",
    "        n = len(words)\n",
    "        res = []\n",
    "        prev_num = 0\n",
    "        for i in range(n):\n",
    "            if words[i] != \"prev\":\n",
    "                prev_num = 0\n",
    "                stack.append(words[i])\n",
    "                \n",
    "            else:\n",
    "                prev_num += 1\n",
    "                if len(stack) != 0:\n",
    "                    if len(stack) < prev_num:\n",
    "                        res.append(-1)\n",
    "                        continue\n",
    "                    res.append(int(stack[-prev_num]))\n",
    "                else:\n",
    "                    res.append(-1)\n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        nums = []\n",
    "        idx = 0\n",
    "        for word in words:\n",
    "            if word == \"prev\":\n",
    "                if idx < len(nums):\n",
    "                    ans.append(nums[idx])\n",
    "                    idx += 1\n",
    "                else:\n",
    "                    ans.append(-1)\n",
    "            else:\n",
    "                nums.insert(0,int(word))\n",
    "                idx = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        nums = []\n",
    "        k = 0\n",
    "        for s in words:\n",
    "            if s[0] != 'p':  # 不是 prev\n",
    "                nums.append(int(s))\n",
    "                k = 0\n",
    "            else:\n",
    "                k += 1\n",
    "                ans.append(-1 if k > len(nums) else nums[-k])  # 倒数第 k 个\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        nums = []\n",
    "        for word in words:\n",
    "            if \"prev\" == word:\n",
    "                cnt +=1\n",
    "                if cnt > len(nums):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(nums[len(nums)-cnt])\n",
    "            else:\n",
    "                cnt = 0\n",
    "                nums.append(int(word))\n",
    "        return ans\n",
    "\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        nums = []\n",
    "        k = 0\n",
    "        for w in words:\n",
    "            if w == 'prev':\n",
    "                k += 1\n",
    "                if k > len(nums):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(nums[::-1][k - 1])\n",
    "            else:\n",
    "                nums.append(int(w))\n",
    "                k = 0\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        def func(words):\n",
    "            length = len(words)\n",
    "            result = []\n",
    "            stack = []\n",
    "\n",
    "            count =0\n",
    "            len_stack =0\n",
    "            for i in range(length):\n",
    "                if words[i]==\"prev\":\n",
    "                    count+=1\n",
    "                    if count<=len_stack:\n",
    "                        result.append(stack[-count])\n",
    "                    else:\n",
    "                        result.append(-1)\n",
    "                else:\n",
    "                    stack.append(eval(words[i]))\n",
    "                    len_stack+=1\n",
    "                    count=0\n",
    "\n",
    "            return result\n",
    "        \n",
    "        return func(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        q = []\n",
    "        cnt = 0\n",
    "        res = []\n",
    "        for x in words :\n",
    "            if x.isdigit():\n",
    "                q.append(int(x))\n",
    "                cnt = 0\n",
    "            else :\n",
    "                cnt += 1\n",
    "                if len(q) < cnt:\n",
    "                    res. append(-1)\n",
    "                else:\n",
    "                    res.append (q[-cnt])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums = []\n",
    "        k = 0\n",
    "        ans = []\n",
    "\n",
    "        for word in words:\n",
    "            if word.isdigit():\n",
    "                nums.append(int(word))\n",
    "                k = 0\n",
    "            else:\n",
    "                k += 1\n",
    "                if k > len(nums):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    val = nums[len(nums) - k]\n",
    "                    ans.append(val)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums_reverse=[]\n",
    "        r=[]\n",
    "        i=0\n",
    "        for w in words:\n",
    "            if w=='prev':\n",
    "                i=i+1\n",
    "                if 1<=i<=len(nums_reverse):\n",
    "                    r.append(nums_reverse[i-1])\n",
    "                else:\n",
    "                    r.append(-1)\n",
    "            else:\n",
    "                i=0\n",
    "                nums_reverse.insert(0,int(w))\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums_reverse=[]\n",
    "        r=[]\n",
    "        i=0\n",
    "        for w in words:\n",
    "            if w=='prev':\n",
    "                i=i+1\n",
    "                if 1<=i<=len(nums_reverse):\n",
    "                    r.append(nums_reverse[i-1])\n",
    "                else:\n",
    "                    r.append(-1)\n",
    "            else:\n",
    "                i=0\n",
    "                nums_reverse.insert(0,int(w))\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        nums = []\n",
    "        k = 0\n",
    "        for s in words:\n",
    "            if s[0] != 'p':  # 不是 prev\n",
    "                nums.append(int(s))\n",
    "                k = 0\n",
    "            else:\n",
    "                k += 1\n",
    "                ans.append(-1 if k > len(nums) else nums[-k])  # 倒数第 k 个\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums_reverse=[]\n",
    "        r=[]\n",
    "        i=0\n",
    "        for w in words:\n",
    "            if w=='prev':\n",
    "                i=i+1\n",
    "                if 1<=i<=len(nums_reverse):\n",
    "                    r.append(nums_reverse[i-1])\n",
    "                else:\n",
    "                    r.append(-1)\n",
    "            else:\n",
    "                i=0\n",
    "                nums_reverse.insert(0,int(w))\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        n = len(words)\n",
    "        num_list = []\n",
    "        res = []\n",
    "        prev_num = 0\n",
    "        for i in range(0, n):\n",
    "            if words[i] == \"prev\":\n",
    "                prev_num += 1\n",
    "                print(\"i:\",i,\", pre_num:\",prev_num,\",num_list:\",num_list)\n",
    "                if len(num_list) >= prev_num:\n",
    "                    res.append(num_list[len(num_list)-prev_num])\n",
    "                else:\n",
    "                    res = res + [-1]\n",
    "            else:\n",
    "                prev_num = 0\n",
    "                num_list.append(int(words[i]))\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        numsave,idx = [],-1\n",
    "        ans = []\n",
    "        for w in words:\n",
    "            if w != 'prev':\n",
    "                numsave.append(int(w))\n",
    "                idx = len(numsave)-1\n",
    "            else:\n",
    "                if idx < 0:\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(numsave[idx])\n",
    "                    idx -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        tmp = []\n",
    "        res = []\n",
    "        cnt = 0\n",
    "        for i in words:\n",
    "            if i.isdigit():\n",
    "                cnt = 0\n",
    "                tmp.append(int(i))\n",
    "            else:\n",
    "                idx = len(tmp)-1-cnt\n",
    "                res.append(-1 if idx < 0 else tmp[idx])\n",
    "                cnt += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        reverse_nums = list()\n",
    "        \n",
    "        ans = list()\n",
    "        \n",
    "        ptr = 0\n",
    "        for w in words:\n",
    "            if w == 'prev':\n",
    "                if ptr > 0:\n",
    "                    ptr -= 1\n",
    "                    ans.append(reverse_nums[ptr])\n",
    "                else:\n",
    "                    ans.append(-1)\n",
    "            else:\n",
    "                reverse_nums.append(int(w))\n",
    "                ptr = len(reverse_nums)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums_reverse=[]\n",
    "        r=[]\n",
    "        i=0\n",
    "        for w in words:\n",
    "            if w=='prev':\n",
    "                i=i+1\n",
    "                if 1<=i<=len(nums_reverse):\n",
    "                    r.append(nums_reverse[i-1])\n",
    "                else:\n",
    "                    r.append(-1)\n",
    "            else:\n",
    "                i=0\n",
    "                nums_reverse.insert(0,int(w))\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        prevCnt=0\n",
    "        nums=[]\n",
    "        n=0\n",
    "        ans=[]\n",
    "        for x in words:\n",
    "            if x == 'prev':\n",
    "                prevCnt +=1\n",
    "                if prevCnt-1<n:\n",
    "                    ans.append(nums[n-prevCnt])\n",
    "                else:\n",
    "                    ans.append(-1)\n",
    "            else:\n",
    "                nums.append(int(x))\n",
    "                n+=1\n",
    "                prevCnt=0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        # 分别定义了存储整数、倒序的整数数组\n",
    "        nums = []\n",
    "        nums_reverse = []\n",
    "        k = 0  # k为到当前位置的连续“prev”字符串数目\n",
    "        res = []  # 输出结果数组\n",
    "        for word in words:\n",
    "            if word != \"prev\":\n",
    "                k = 0\n",
    "                nums.append(int(word))\n",
    "            else:\n",
    "                nums_reverse = nums[::-1]\n",
    "                k += 1\n",
    "                if len(nums_reverse) <= k - 1:\n",
    "                    res.append(-1)\n",
    "                else:\n",
    "                    res.append(nums_reverse[k - 1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums_reverse=[]\n",
    "        r=[]\n",
    "        i=0\n",
    "        for w in words:\n",
    "            if w=='prev':\n",
    "                i=i+1\n",
    "                if 1<=i<=len(nums_reverse):\n",
    "                    r.append(nums_reverse[i-1])\n",
    "                else:\n",
    "                    r.append(-1)\n",
    "            else:\n",
    "                i=0\n",
    "                nums_reverse.insert(0,int(w))\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums_reverse=[]\n",
    "        r=[]\n",
    "        i=0\n",
    "        for w in words:\n",
    "            if w=='prev':\n",
    "                i=i+1\n",
    "                if 1<=i<=len(nums_reverse):\n",
    "                    r.append(nums_reverse[i-1])\n",
    "                else:\n",
    "                    r.append(-1)\n",
    "            else:\n",
    "                i=0\n",
    "                nums_reverse.insert(0,int(w))\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        lst = []\n",
    "        curr = -1\n",
    "        for ch in words:\n",
    "            if ch == 'prev':\n",
    "                if curr == -1:\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(lst[curr])\n",
    "                    curr -= 1\n",
    "            else:\n",
    "                num = int(ch)\n",
    "                lst.append(num)\n",
    "                curr = len(lst) - 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        k = 0\n",
    "        ans = []\n",
    "        reverse_num = []\n",
    "        for i in range(len(words)):\n",
    "            word = words[i]\n",
    "            if word == \"prev\":\n",
    "                if k >= len(reverse_num):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(reverse_num[k])\n",
    "                k += 1\n",
    "            else:\n",
    "                k = 0\n",
    "                reverse_num.insert(0, int(word))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        p=-1\n",
    "        nums_reverse=[]\n",
    "        res=[]\n",
    "        for i in words:\n",
    "            if i==\"prev\":\n",
    "                p+=1\n",
    "                if p<len(nums_reverse):\n",
    "                    res.append(int(nums_reverse[p]))\n",
    "                else:\n",
    "                    res.append(-1)\n",
    "            if i!=\"prev\":\n",
    "                p=-1\n",
    "                nums_reverse.insert(0,i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums = []\n",
    "        prev_count = 0\n",
    "        result = []\n",
    "\n",
    "        for word in words:\n",
    "            if word == \"prev\":\n",
    "                prev_count += 1\n",
    "                if prev_count <= len(nums):\n",
    "                    result.append(nums[-prev_count])\n",
    "                else:\n",
    "                    result.append(-1)\n",
    "            else:\n",
    "                num = int(word)\n",
    "                nums.append(num)\n",
    "                prev_count = 0\n",
    "\n",
    "        return result\n",
    "words = [\"1\",\"prev\",\"2\",\"prev\",\"prev\"]\n",
    "Solution().lastVisitedIntegers(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        s = []\n",
    "        n = len(words)\n",
    "        res = []\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if words[i] != \"prev\":\n",
    "                s.append(int(words[i]))\n",
    "                i += 1\n",
    "            else:\n",
    "                j = i + 1\n",
    "                while j < n and words[j] == \"prev\":\n",
    "                    j += 1\n",
    "                for k in range(i, j):\n",
    "                    if (k - i) >= len(s):\n",
    "                        res.append(-1)\n",
    "                    else:\n",
    "                        res.append(s[len(s) - 1 - (k - i)])\n",
    "                i = j\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        ans = []\n",
    "        nums = []\n",
    "        k = 0\n",
    "        for s in words:\n",
    "            if s[0] != 'p':  # 不是 prev\n",
    "                nums.append(int(s))\n",
    "                k = 0\n",
    "            else:\n",
    "                k += 1\n",
    "                ans.append(-1 if k > len(nums) else nums[-k])  # 倒数第 k 个\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        '''\n",
    "        nums : 遍历到的数字数组\n",
    "        nums_reverse : 反转遍历到的数组\n",
    "        prev: 连续的‘prev’的数量\n",
    "        '''\n",
    "        nums = []\n",
    "        count = 0\n",
    "        result = []\n",
    "        for word in words:\n",
    "            try:\n",
    "                word = int(word)\n",
    "                nums.append(word)\n",
    "                count = 0\n",
    "            except:\n",
    "                count += 1\n",
    "            finally:\n",
    "                if count > 0:\n",
    "                    if (count - 1) < len(nums):\n",
    "                        result.append(nums[-count])\n",
    "                    else:\n",
    "                        result.append(-1)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums_reverse=[]\n",
    "        r=[]\n",
    "        i=0\n",
    "        for w in words:\n",
    "            if w=='prev':\n",
    "                i=i+1\n",
    "                if 1<=i<=len(nums_reverse):\n",
    "                    r.append(nums_reverse[i-1])\n",
    "                else:\n",
    "                    r.append(-1)\n",
    "            else:\n",
    "                i=0\n",
    "                nums_reverse.insert(0,int(w))\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        stack, pos, res = [], -1, []\n",
    "        for x in words:\n",
    "            if x == \"prev\":\n",
    "                if pos == -1:\n",
    "                    res.append(pos)\n",
    "                else:\n",
    "                    res.append(stack[pos])\n",
    "                    pos -= 1\n",
    "            else:\n",
    "                pos = len(stack)\n",
    "                stack.append(int(x))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums = []\n",
    "        k = 0\n",
    "        ans = []\n",
    "        for i in range(len(words)):\n",
    "            if words[i] != \"prev\":\n",
    "                nums.append(int(words[i]))\n",
    "                k = 0\n",
    "            else:\n",
    "                if k == 0:\n",
    "                    if len(nums) != 0:\n",
    "                        ans.append(int(words[i - 1]))\n",
    "                    else:\n",
    "                        ans.append(-1)\n",
    "                    k += 1\n",
    "                else:\n",
    "                    tmp = list(reversed(nums))\n",
    "                    if k < len(nums):\n",
    "                        ans.append(tmp[k])\n",
    "                    else:\n",
    "                        ans.append(-1)\n",
    "                    k += 1\n",
    "        return ans\n",
    "                    \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums_reverse=[]\n",
    "        r=[]\n",
    "        i=0\n",
    "        for w in words:\n",
    "            if w=='prev':\n",
    "                i=i+1\n",
    "                if 1<=i<=len(nums_reverse):\n",
    "                    r.append(nums_reverse[i-1])\n",
    "                else:\n",
    "                    r.append(-1)\n",
    "            else:\n",
    "                i=0\n",
    "                nums_reverse.insert(0,int(w))\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        k = 0;\n",
    "        nums = []\n",
    "        res = []\n",
    "        for word in words:\n",
    "            if word != 'prev':\n",
    "                k = 0\n",
    "                nums.append(int(word))\n",
    "            else:\n",
    "                k += 1\n",
    "                numr = nums[::-1]\n",
    "                if k > len(nums):\n",
    "                    res.append(-1)\n",
    "                else:\n",
    "                    res.append(numr[k-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        k=0             #连续prev 的数目\n",
    "        nums=[]         #当前遍历过整数\n",
    "        ans=[]\n",
    "        for i in words:\n",
    "            if i != 'prev':\n",
    "                nums.append(int(i))\n",
    "                k=0\n",
    "            else:\n",
    "                k+=1\n",
    "                if k>len(nums):\n",
    "                    ans.append(-1)\n",
    "                else:\n",
    "                    ans.append(nums[len(nums)-k])\n",
    "                \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "class Solution:\n",
    "    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n",
    "        nums = []\n",
    "        prev_count = 0\n",
    "        result = []\n",
    "\n",
    "        for word in words:\n",
    "            if word == \"prev\":\n",
    "                prev_count += 1\n",
    "                if prev_count <= len(nums):\n",
    "                    result.append(nums[-prev_count])\n",
    "                else:\n",
    "                    result.append(-1)\n",
    "            else:\n",
    "                num = int(word)\n",
    "                nums.append(num)\n",
    "                prev_count = 0\n",
    "\n",
    "        return result\n",
    "words = [\"1\",\"prev\",\"2\",\"prev\",\"prev\"]\n",
    "Solution().lastVisitedIntegers(words)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
